package com.allinpay.bizroute.proto;

import com.allinpay.bizroute.genproto.MerchantTermSsn;
import com.allinpay.bizroute.genproto.RouteDetail;
import com.allinpay.bizroute.genproto.RouteRule;
import com.allinpay.bizroute.genproto.UpdateRouteSvcGrpc;
import io.grpc.stub.StreamObserver;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Random;

/**
 * @ClassName UpdRouteSvc
 * @Description UpdRouteSvc
 * @Author richard
 * @Date 2021/12/15 5:45 PM
 */
@Slf4j
@Component
public class UpdRouteSvc extends UpdateRouteSvcGrpc.UpdateRouteSvcImplBase {
    @Override
    public void updByMchtTerm(MerchantTermSsn request, StreamObserver<RouteRule> responseObserver) {
        String mch = request.getMch();
        String tcd = request.getTcd();
        int ssn = request.getSsn();
        log.info("proc UpdRotueSvcImpl,mcd:{},tcd:{},ssn:{}");
        RouteRule routeRule = updateRouteRule(mch, tcd, ssn);

        responseObserver.onNext(routeRule);
        responseObserver.onCompleted();
    }

    private RouteRule updateRouteRule(String mch, String tcd, int ssn) {
        Random random = new Random();
        RouteDetail routeDetail = RouteDetail.newBuilder().setName("updateRouteRule")
                .setNum(random.nextInt(100)).build();

        RouteRule routeRule = RouteRule.newBuilder().setRspCd(200)
                .setDetail(routeDetail)
                .build();

        return routeRule;
    }
}

